/*
 This file 'Formatter' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 15:54
 */

define(function (require, exports, module) {

//	var tmpl = require("tmpl");
	var Component = require("Firebird.ui.Component");

	/**
	 * Firebird.ui.Formatter
	 * @class Firebird.ui.Formatter
	 */
	Firebird.define("Firebird.ui.Formatter", {
		extend: "Firebird.ui.Component",
		renderCheckbox: function (row) {
			var tpl = ["<input type='checkbox' name='gridCheckboxRadio' value='<?tpl=id?>' "];
			for (var key in row) {
				if (row.hasOwnProperty(key)) {
					tpl.push("data-" + key + "='<?tpl=" + key + "?>' ");
				}
			}
			tpl.push(" />");
			// firebird.info(firebird.tpl.render(tpl.join(""), row));
//			return tmpl(tpl.join(""))(row);
			return firebird.tpl.render(tpl.join(""), row);
		},
		renderRadio: function (row) {
			var tpl = ["<input type='radio' name='gridCheckboxRadio' value='<?tpl=id?>' "];
			for (var key in row) {
				if (row.hasOwnProperty(key)) {
					tpl.push("data-" + key + "='<?tpl=" + key + "?>' ");
				}
			}
			tpl.push(" />");
			// firebird.info(firebird.tpl.render(tpl.join(""), row));
			return firebird.tpl.render(tpl.join(""), row);
		},
		edit: '<span class="ui-icon ui-icon-pencil" title="修改"></span>',
		del: '<span class="ui-icon ui-icon-trash" title="删除"></span>',
		plus: '<span class="ui-icon ui-icon-plus" title="新增行"></span>',
		blank: '<span class="ui-icon ui-icon-blank" title=""></span>',
		_lastTicket: 0,
		checkbox: "<input type='checkbox' name='gridCheckboxRadio' value='<?tpl=id?>' " +
			"data-pid='<?tpl=pid?>' " +
			"data-id='<?tpl=id?>' " +
			"pid='<?tpl=pid?>' /> ",
		radio: "<input type='radio' name='gridCheckboxRadio' value='<?tpl=id?>' " +
			"data-pid='<?tpl=pid?>' " +
			"data-id='<?tpl=id?>' " +
			"pid='<?tpl=pid?>' /> ",
		operating: function (operating) {
			var tmp = "";
			var formatter = this;
			$.each(operating, function (i, n) {
				if (firebird.isObject(n)) {
					var k = null,
						v = null;
					for (k in n) {
						v = n[k];
					}
					tmp += $.isFunction(v) ? v(formatter[k]) : v;
				} else {
					tmp += formatter[n];
				}
			});
			window._formatterseed = tmp;
			return this.wrap;
		},
		setButtons: function (btns) {
			var tmp = "";
			$.each(btns, function (i, n) {
				tmp += '<span class="ui-icon ui-icon-' + (n.icon || "blank") + '" title="' + (n.title || "") + '"></span>';
			});
			window._formatterseed = tmp;
			return this.wrap;
		},
		wrap: function (val, opt, row) {
//			var data = [];
//			for (var key in row) {
//				data.push('data-' + key + "='"+row[key]+"'");
//			}
			var html_con =
					'<div class="operating" data-id="' + row.id + '">' +
					window._formatterseed +
					'</div>';
			return html_con;
		},
		setLinks: function () {

		},
		setHtml: function () {

		},
		/**
		 *
		 */
		setTpl: function (id, source) {
			return firebird.tpl.render.apply(firebird.tpl, arguments);
		}
	});

	module.exports = Firebird.ui.Formatter;

});